home *** CD-ROM | disk | FTP | other *** search
- Newsgroups: comp.lang.c++
- Path: news.sprintlink.net!mv!usenet
- From: ENGR@GSSI.MV.COM (Michael Furman)
- Subject: Re: Saving a C++ object
- Message-ID: <DnJw65.AM3@mv.mv.com>
- Mime-Version: 1.0
- Organization: GSSI
- Date: Thu, 29 Feb 1996 18:46:52 GMT
- References: <3135C74E.446B9B3D@doc.ic.ac.uk>
- X-Newsreader: WinVN 0.93.10
- X-Nntp-Posting-Host: gssi.mv.com
-
- In article <3135C74E.446B9B3D@doc.ic.ac.uk>, brj@doc.ic.ac.uk says...
- >
- >When I save a C++ object just using:
- >
- >----------------------------
- >class ovoid{
- >/* etc. */
- >}
- >
- >ovoid square
- >
- >file.write((unsigned char *) &square, sizeof(ovoid))
- >----------------------------
- >
- >What *exactly* is saved? I'm finding that every time I recompile
- >an application when I've added some new code, trying to load
- >an object saved in this way with an old version of the application
- >crashes it, even if the class of the object in question hasn't
- >changed. I presume there are some pointers or something which are
- >changing when I add new chunks of code. What can I do to avoid this?
- >
- >(ovoid doesn't containg any explicit pointers which are not reset when
- >reloading - ie. I'm not trying to use old pointers which have
- >been saved - not to my knowledge anyway)
-
- Your class probably has virtual member functions. You can do such read /
- write operations only for classes that mapped to memory as regular "C"
- structures. Such classes cannot have virtual functions and base classes.
-
- >
- >Also, is there anything dodgy about saving a class from within itself,
- >using:
- >
- >file.write((unsigned char *) &this, sizeof(ovoid))
-
- Nothing except bug you made. "this" is a pointer to the class instance, not
- the instance or reference. And your line must be:
-
- file.write((char *)this, sizeof(ovoid));
-
- And be sure that your stream (fine) onened in binary mode.
-
-
-
- >
- >Thanks for your help.
- >
- >--
- >Je suis triste et seul ici.
-
- --
- <<< If you received it by E-mail: it is a copy of post to the newsgroup >>>
- ---------------------------------------------------------------
- Michael Furman, (603)893-1109
- Geophysical Survey Systems, Inc. fax:(603)889-3984
- 13 Klein Drive - P.O. Box 97 engr@gssi.mv.com
- North Salem, NH 03073-0097 71543.1334@compuserve.com
- ---------------------------------------------------------------
-
-